package utils

import (
	"bytes"
	"crypto/rand"
	// "encoding/binary"
	"golang.org/x/crypto/scrypt"
)

// Encode scrypt 加密
func Encode(pwd []byte) (enpwd []byte, salt []byte, err error) {
	salt = make([]byte, 32, 32)
	rand.Read(salt)
	enpwd, err = scrypt.Key(pwd, salt, 16384, 8, 1, 32)
	return
}

// Compare scrypt 比对
func Compare(bpwd []byte, pwd []byte, salt []byte) bool {
	if enpwd, err := scrypt.Key(pwd, salt, 16384, 8, 1, 32); err != nil {
		return false
	} else {
		return bytes.Compare(bpwd, enpwd) == 0
	}
}
